﻿using Microsoft.AspNetCore.Mvc;
using System.Threading.Tasks;
using Volo.Abp.Domain.Repositories;
using System.Linq;
using Microsoft.EntityFrameworkCore;
using ERP.ERPModels.Production.MasterData;
using ERP.HumanResources.Production.MasterData;
using ERP.HumanResources.HumanResourceManagement.Dtos;
using AutoMapper.Internal.Mappers;
using ERP.RBAC.Dto;

namespace ERP.Web.Controllers.Production.MasterData
{
    public class ProcessConfigurationController : Controller
    {
        private readonly IRepository<ProcessConfiguration, int> _processConfigurationRepository;
        private readonly IProcessConfigurationService _processConfigurationService;

        public ProcessConfigurationController(IRepository<ProcessConfiguration, int> processConfigurationRepository, IProcessConfigurationService processConfigurationService)
        {
            _processConfigurationRepository = processConfigurationRepository;
            _processConfigurationService = processConfigurationService;
        }

        /// <summary>
        /// 工序设置
        /// </summary>
        /// <returns></returns>
        public IActionResult ProcessConfigurationIndex()
        {
            return View();
        }

        /// <summary>
        /// 显示工序配置分页列表
        /// </summary>
        /// <param name="param">查询参数</param>
        /// <returns>分页结果</returns>
        [HttpGet]
        public async Task<IActionResult> GetProcessList([FromQuery]ProcessConfigurationPageListDTO param)
        {
            // 1. 构建查询
            var query = await _processConfigurationRepository
                .GetQueryableAsync();

            // 2. 应用筛选条件
            if (!string.IsNullOrEmpty(param.ProcedureCode))
            {
                query = query.Where(x => x.ProcedureCode.Contains(param.ProcedureCode));
            }

            if (!string.IsNullOrEmpty(param.ProcedureName))
            {
                query = query.Where(x => x.ProcedureName.Contains(param.ProcedureName));
            }

            if (param.IsNo != 3)
            {
                query = query.Where(x => x.IsNo == param.IsNo);
            }

            // 3. 计算总记录数
            var totalCount = await query.CountAsync();

            // 4. 执行分页查询
            var items = await query
                .OrderBy(x => x.ProcedureCode)
                .Skip((param.PageIndex - 1) * param.PageSize)
                .Take(param.PageSize)
                .ToListAsync();

            // 5. 构建返回结果
            return Json(new { 
                code = 0, 
                msg = "success", 
                count = totalCount,
                data = items
            });
        }

        /// <summary>
        /// 新增工序
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> AddProcessConfiguration([FromBody]ProcessConfigurationAddDTO param)
        {
            var result = await _processConfigurationService.AddProcessConfiguration(param);
            return Json(new { success = result > 0, message = result > 0 ? "添加成功" : "添加失败" });
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> UpdateProcessConfiguration(ProcessConfigurationUpdateDTO param)
        {
            var result = await _processConfigurationService.UpdateProcessConfiguration(param);
            return Json(new { success = result > 0, message = result > 0 ? "修改成功" : "修改失败" });
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> DeleteProcessConfiguration(int id)
        {
            await _processConfigurationService.DeleteProcessConfiguration(id);
            return Json(new { success = true, message = "删除成功" });
        }
    }
}
